package services.intermediate

import models.baseinfo._
import play.api.Logging
import play.api.cache.SyncCacheApi

import javax.inject.Inject
import scala.concurrent.duration.{Duration, DurationInt}
import scala.concurrent.{Await, ExecutionContext}

class BaseInfomation @Inject()(
                                itemCodeRepo: GYItemCodeRepository,
                                stockCodeRepo: KDStockCodeRepository,
                                cache: SyncCacheApi
                              )(implicit exec: ExecutionContext)
  extends Logging {


  //# 金蝶仓库代码
  def getKdStock() = {
    cache.getOrElseUpdate[Seq[KDStockCode]]("baseInfo.getKdStockCode", 1.hours) {
      Await.result(stockCodeRepo.list, Duration.Inf)
    }
  }

  def getKdMaterial() = {
    cache.getOrElseUpdate[Seq[GYItemCode]]("baseInfo.getKdMaterial", 1.hours) {
      Await.result(itemCodeRepo.list, Duration.Inf)
    }
  }

  def getKdStockCode(code: String):KDStockCode = {
    val codeList = this.getKdStock().filter(f => f.kdFTHIRDSTOCKNO == code)
    if (codeList.isEmpty) KDStockCode() else codeList.head
  }

  def getKdMaterialCode(code: String):GYItemCode = {
    val codeList = this.getKdMaterial().filter(f => f.gyCode == code)
    if (codeList.isEmpty) GYItemCode(kdNumber = code,kdBaseUnit = "Pcs") else codeList.head
  }

}
